package longrise.phone.com.bjjt_jyb.compensate;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;

import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.Constant.URLConstant;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;
import longrise.phone.com.bjjt_jyb.Utils.UiUtils;


/**
 * 阅读是否符合快撤快赔的条件温馨提示，不符合提示报警，Created by Administrator on 2016/5/12.
 */

public class PromptView extends LFView implements View.OnClickListener {
    private Context context = null;
    private View view = null;
    private Button bt_prompt;
    private Button bt_prompt_callpolice;
    private BaseApplication mApplication;
    private Dialog processDialog;
    private LinearLayout hn_btn_back;
    private TextView tv_title;

    public PromptView(Context c) {
        super(c);
        this.context = c;
        this.mApplication = (BaseApplication) c.getApplicationContext();
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_prompt, null);
        bt_prompt = (Button) view.findViewById(R.id.bt_prompt);
        bt_prompt_callpolice = (Button) view.findViewById(R.id.bt_prompt_callpolice);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("温馨提示");
    }

    private void regEvent(boolean b) {
        if (null != bt_prompt) {
            bt_prompt.setOnClickListener(b ? this : null);
        }
        if (null != bt_prompt_callpolice) {
            bt_prompt_callpolice.setOnClickListener(b ? this : null);
        }
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hn_btn_back:
                closeForm();
                OnDestroy();
                break;
            case R.id.bt_prompt:
                CreateAppCaseNumber();
                break;
            case R.id.bt_prompt_callpolice:
                //报警
                UiUtils.showDialog(getContext(), null, "提示",
                        "请您立即拨打110报警，报警后在现场等待警察处理", "取消", "拨打110",
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {

                            }
                        }, new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                //LSMsgCall(Constant.DAIL122);
                                // 拨打122
                                Intent intent = new Intent(Intent.ACTION_DIAL, Uri
                                        .parse("tel:110"));
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                getContext().startActivity(intent);
                                //closeForm();
                                //OnDestroy();
                            }
                        });

                break;

        }
    }
    // 生成唯一档案编号
    private void CreateAppCaseNumber() {
        try {
            processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
            EntityBean bean = new EntityBean();
            String userName = mApplication.getLoginInfo().getBean("userinfo")
                    .getString("userflag");
            bean.set("areaid",mApplication.getAreaid());
            //bean.set("areaid","410101000000000000");
            bean.set("username",userName);
            bean.set("token",mApplication.getToken());
            LoadDataManagerCar.getInstance(context).callService(null,
                    mApplication.getServerUrl(), URLConstant.KCKPCREATEAPPCASENO, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                        @Override
                        public void onSuccess(String key, String service, Object result) {
                            if (null != processDialog)
                            {
                                if (processDialog.isShowing())
                                {
                                    processDialog.dismiss();
                                }
                            }
                            EntityBean bean = (EntityBean) result;
                            String restate = bean.getString("restate");
                            if ("1".equals(restate)) {
                                EntityBean data = bean.getBean("data");
                                String appCaseNo = data.getString("appcaseno");
                                // 调用全局服务将案件类型和案件no保存在accidentview
                               /* LSMsgCall(Constant.CASENO, accidentType,
                                        appCaseNo);
                                FrameworkManager.getInstance().showNewForm(
                                        getContext(),
                                        new XJScenePicturesView(context, id,
                                                appCaseNo));*/
                                ChooseAccidentTypeView chooseaccidenttypeview = new ChooseAccidentTypeView(context,appCaseNo);
                                FrameworkManager.getInstance().showNewForm(context, chooseaccidenttypeview);
                            } else {
                                UiUtil.showToast(context,"创建案件编号失败");
                            }
                        }

                        @Override
                        public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                            if (null != processDialog)
                            {
                                if (processDialog.isShowing())
                                {
                                    processDialog.dismiss();
                                }
                            }
                            UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                        }

                        @Override
                        public void onFinished(String key, String service) {
                            if (null != processDialog)
                            {
                                if (processDialog.isShowing())
                                {
                                    processDialog.dismiss();
                                }
                            }
                        }
                    });
        } catch (Exception e) {

            e.printStackTrace();
        }
    }




}
